﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StoreTransport.DAO;
using StoreTransport.Resources;
using StoreTransport.Business.Helper;
namespace StoreTransport.Presentation.Forms {
    public partial class ASMListForm : Form {
        int id = 0;
        public ASMListForm() {
            InitializeComponent();
        }

        private void ASMList_Load(object sender, EventArgs e) {
            _tbxASMName.Select();
            LoadASMs();
        }

        private void LoadASMs() {
            StoreTransportDataContext context = CommonHelper.GetContext();
            _grdASM.DataSource = context.ASMs.Cast<ASM>().OrderBy(c => c.ASMName);
        }

        private void _btxCreate_Click(object sender, EventArgs e) {
            _tbxASMName.Select();
            _tbxASMName.Text = string.Empty;
            _lblID.Text = StoreTransportResource.Form_NewID;
            id = 0;
        }

        private void _btxSave_Click(object sender, EventArgs e) {
            StoreTransportDataContext context = CommonHelper.GetContext();
            string errorMessage = CommonHelper.CheckRequiredField(_tbxASMName.Text, _lblASMName.Text);
            if (!string.IsNullOrEmpty(errorMessage)) {
                MessageBox.Show(errorMessage);
                return;
            }
            if (id != 0) {
                var item = context.ASMs.FirstOrDefault(c => c.ASMName.Equals(_tbxASMName.Text));
                if (item != null && item.ID != id) {
                    MessageBox.Show(string.Format(StoreTransportResource.Error_Dupplicate_ASM, _tbxASMName.Text));
                    return;
                }
                ASM selectedItem = context.ASMs.Where(c => c.ID == id).First();
                selectedItem.ASMName = _tbxASMName.Text;
            } else {
                if (context.ASMs.FirstOrDefault(c => c.ASMName.Equals(_tbxASMName.Text)) != null) {
                    MessageBox.Show(string.Format(StoreTransportResource.Error_Dupplicate_ASM, _tbxASMName.Text));
                    return;
                }
                ASM item = new ASM();
                item.ASMName = _tbxASMName.Text;
                context.ASMs.InsertOnSubmit(item);
            }
            context.SubmitChanges();
            LoadASMs();
        }

        private void _btxEdit_Click(object sender, EventArgs e) {
            if (_grdASM.SelectedRows.Count > 0) {
                id = int.Parse(_grdASM.SelectedRows[0].Cells[0].Value.ToString());
                EditItem();
            }
        }

        private void EditItem() {
            ASM selectedItem = CommonHelper.GetContext().ASMs.Where(c => c.ID == id).First();
            _tbxASMName.Text = selectedItem.ASMName;
            _lblID.Text = id.ToString();
        }

        private void _btnDelete_Click(object sender, EventArgs e) {
            if (_grdASM.SelectedRows.Count > 0) {
                DialogResult result = MessageBox.Show(StoreTransportResource.Message_ConfirmDelete,
                    StoreTransportResource.MessageBox_ConfirmDelete, MessageBoxButtons.YesNo);
                if (result == System.Windows.Forms.DialogResult.Yes) {
                    try {
                        StoreTransportDataContext context = CommonHelper.GetContext();
                        foreach (DataGridViewRow row in _grdASM.SelectedRows) {
                            id = int.Parse(row.Cells[0].Value.ToString());
                            ASM selectedItem = context.ASMs.Where(c => c.ID == id).First();
                            context.ASMs.DeleteOnSubmit(selectedItem);
                            context.SubmitChanges();
                        }
                        LoadASMs();
                        id = 0;
                    } catch (Exception ex) {
                        MessageBox.Show(StoreTransportResource.ErrorMessage_CanNotDeleteItem);
                    }
                }
            } else {
                MessageBox.Show(StoreTransportResource.NoItemSelected);
            }
        }

        private void _grdASM_CellDoubleClick(object sender, DataGridViewCellEventArgs e) {
            id = int.Parse(_grdASM.Rows[e.RowIndex].Cells[0].Value.ToString());
            EditItem();
        }
    }
}
